package com.jike.noobmoney.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.TaskCategoryAdapter;
import com.jike.noobmoney.adapter.TaskListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.CateListEntity;
import com.jike.noobmoney.entity.TaskListEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.widget.CheckableTextView;
import com.jike.noobmoney.mvp.view.widget.MyGridView;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchTastListActivity extends BaseActivity implements IView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.et_search)
    EditText et_search;

    private TaskListAdapter adapter;

    private List<TaskListEntity.TaskBean> mData = new ArrayList<>();
    private TaskPresenter taskPresenter;
    private String userid = "";
    private ImageView iv_back;
    public String value;

    @Override
    protected int layoutResID() {
        return R.layout.activity_search_task_list;
    }
    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }
    @Override
    protected void initData() {
        taskPresenter = new TaskPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        adapter = new TaskListAdapter(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        //item点击
        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            TaskListEntity.TaskBean item = (TaskListEntity.TaskBean) adapter.getItem(position);
            Intent intent = new Intent(context, TaskDetailActivity.class);
            intent.putExtra(TaskDetailActivity.t_id, "" + item.getT_id());
            startActivity(intent);
        });
        initView();
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

        }
    }

    /**
     * 获取数据
     */
    private void getData(String value) {
        showProgress();
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        taskPresenter.getSearchTasklist(userid,value, ConstantValue.RequestKey.task_list);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.task_list.equals(actionType)) {
            List<TaskListEntity.TaskBean> task = (List<TaskListEntity.TaskBean>) data;
            if (task != null && task.size() > 0) {
                    mData.clear();
                    mData.addAll(task);
            }
            adapter.refresh(mData);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void initView() {
        et_search.setOnKeyListener(new View.OnKeyListener() {// 输入完后按键盘上的搜索键

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {// 修改回车键功能
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    // 按完搜索键后将当前查询的关键字保存起来,如果该关键字已经存在就不执行保存

                    // TODO 根据输入的内容模糊查询商品，并跳转到另一个界面，由你自己去实现
                    String value=et_search.getText().toString();
                    mData.clear();
                    getData(value);
                }
                return false;
            }
        });
    }


    private void disLoading() {
        dismissProgress();
    }


}
